using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessRules;
using DataAccess;

public partial class SigAcciones : PageBase
{


    public string StrPlnId
    {
        get
        {
            if (ViewState["strPlnId"] != null)
                return ViewState["strPlnId"].ToString();
            else
                return "";
        }
        set { ViewState["strPlnId"] = value; }
    }

    public string StrAccId
    {
        get
        {
            if (ViewState["strAccId"] != null)
                return ViewState["strAccId"].ToString();
            else
                return "";
        }
        set { ViewState["strAccId"] = value; }
    }
    private DataTable SigSigAccionDataTable
    {

        get
        {

            DataTable objTable = null;
            if (Cache[SigAccionData.SIG_ACCION_TABLE + Session.SessionID] != null)
                objTable = (DataTable)Cache[SigAccionData.SIG_ACCION_TABLE + Session.SessionID];
            return objTable;
        }

        set
        {
            if (value != null)
            {
                Cache.Insert(SigAccionData.SIG_ACCION_TABLE + Session.SessionID, value);
            }
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        traerContextItems();
        if (!IsPostBack)
        {
            if (StrPlnId != "")
            {
                cargarCampos();
                BindDatosDependencia();
            }
        }

        if (SigSigAccionDataTable != null)
        {
            gidvAcciones.DataSource = SigSigAccionDataTable.DefaultView;
            gidvAcciones.DataBind();
        }

        gidvAcciones.GroupBy(gidvAcciones.Columns["ACTIVIDAD"]);
        

    }

    private void cargarCampos()
    {
        SigPlanAccion objSigPlanAccion = new SigPlanAccion(ConfigurationManager.AppSettings["Conn"].ToString());
        SigPlanAccionData objSigPlanAccionData = objSigPlanAccion.ConsultarPnlId(Convert.ToDecimal(StrPlnId));
        if(objSigPlanAccionData !=  null)
        {
            DataRow fila = objSigPlanAccionData.Rows[0];
            lblDependencia.Text = fila[SigPlanAccionData.DEP_NOMBRE_FIELD].ToString();
            lblDescripcionRubro.Text = fila[SigPlanAccionData.PLN_NOMBRE_FIELD].ToString();
            lblCodigoPresupuestal.Text = fila[SigPlanAccionData.PLN_CODIGO_PRESUPUESTAL_FIELD].ToString();
            decimal decVAlor = Convert.ToDecimal(fila[SigPlanAccionData.PLN_VALOR_APROPIACION_FIELD].ToString());
            lblValorApropiacion.Text = decVAlor.ToString("C");
 
        }



  
    }

    private void BindDatosDependencia()
    {
        SigAccion objSigAccion = new SigAccion(ConfigurationManager.AppSettings["Conn"]);
        SigAccionData objSigAccionData = objSigAccion.ConsultarAccionPlnIDListado(StrPlnId);
        SigSigAccionDataTable = objSigAccionData;
        gidvAcciones.DataSource = objSigAccionData.DefaultView;
        gidvAcciones.DataBind();

    }



    #region ContextItems

    private void crearContextItems()
    {
        Context.Items["accId"] = StrAccId;
        Context.Items["plnId"] = StrPlnId;
    }

    private void traerContextItems()
    {
        if (Context.Items["plnId"] != null)
        {
            StrPlnId = Convert.ToString(Context.Items["plnId"]);
        }
    }

    #endregion


    protected void grivSigAccion_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gidvAcciones.PageIndex = e.NewPageIndex;

        if (SigSigAccionDataTable != null)
        {
            gidvAcciones.DataSource = SigSigAccionDataTable.DefaultView;
            gidvAcciones.DataBind();
        }
    }

    protected void aspxgidvContratos_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        if (e.KeyValue.ToString() != "")
        {
            StrAccId = e.KeyValue.ToString();
            crearContextItems();
            if (e.CommandArgs.CommandName.ToString() == "ver")
            Server.Transfer("SigAccionMetas.aspx", true);
            if (e.CommandArgs.CommandName.ToString() == "verzonas")
                Response.Write("<script language=javascript>window.open('SigEjeAccionesZonasGeograficasList.aspx?AccId=" + StrAccId + "&PlnId=" + StrPlnId + "','nueva_ventana','width=800,height=600,scrollbars=1');</script>");
        }
    }
    protected void btnRegresar_Click(object sender, ImageClickEventArgs e)
    {
        Server.Transfer("EjecucionPresupuestal.aspx?SSM_ID=13", true);
    }

    protected void lnkAccionesMetas_Click(object sender, EventArgs e)
    {
        List<object> keyValues = gidvAcciones.GetSelectedFieldValues("ACC_ID");
        foreach (object key in keyValues)
        {
            StrAccId = key.ToString();
        }
        crearContextItems();
        Server.Transfer("SigAccionMetas.aspx?SSM_ID=13", true);
    }
    protected void LinkEpes_Click(object sender, EventArgs e)
    {
        List<object> keyValues = gidvAcciones.GetSelectedFieldValues("ACC_ID");
        foreach (object key in keyValues)
        {
            StrAccId = key.ToString();
        }
        crearContextItems();
        Server.Transfer("AccionEpe.aspx?SSM_ID=13", true);
    }
    protected void LinkContratos_Click(object sender, EventArgs e)
    {
        List<object> keyValues = gidvAcciones.GetSelectedFieldValues("ACC_ID");
        foreach (object key in keyValues)
        {
            StrAccId = key.ToString();
        }
        crearContextItems();
        Server.Transfer("Contratos.aspx?SSM_ID=13", true);
    }
    protected void lblDescripcionRubro_Click(object sender, EventArgs e)
    {
        Server.Transfer("EjecucionPresupuestal.aspx", true);
    }

    protected void btnXlsExport_Click(object sender, EventArgs e)
    {
        gridExport.WriteXlsToResponse();
    }

    protected void btnRtfExport_Click(object sender, EventArgs e)
    {
        gridExport.WriteRtfToResponse();
    }


    protected void btnCsvExport_Click(object sender, EventArgs e)
    {
        gridExport.WriteCsvToResponse();
    }


}
